package com.cow.framework.core.message;

import java.io.Serializable;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;



public interface MessageDAO extends JpaRepository<Message, Serializable>{
	Page<Message> findByReceiver(Integer receiver,Pageable pageable);
	@Query("select m from Message m where status = ?1 and sendTime > CURRENT_TIMESTAMP() - 1000000 ")
	List<Message> findByStatus(Integer status);
	List<Message> findByReceiverAndStatus(Integer receiver,Integer status);
	@Query("select m from Message m where receiver = ?1 and status <> 2 order by sendTime desc")
	Page<Message> findNoRead(Integer receiver,Pageable pageable);
	@Query("select count(1) from Message m where receiver = ?1 and status <> 2")
	Integer countNoRead(Integer receiver);
	
	@Modifying
	@Query(value="delete from message where CURRENT_TIMESTAMP() - send_time > 30000000 ",nativeQuery=true)
	int delExpiredMessage();
}